﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using GSLMS.Entity;

namespace GSLMS.WebAPP
{
    public static class MyHtmlHelper
    {

        public static MvcHtmlString FormatHtml(object sourcehtml)
        {
            string html = string.Empty;
            if (sourcehtml != null)
            { html = sourcehtml.ToString(); }
            else
            {
                return new MvcHtmlString(null);
            }
            System.Text.RegularExpressions.Regex regex1 =
                     new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 =
                  new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 =
                  new System.Text.RegularExpressions.Regex(@" no[sS]*=",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 =
                  new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 =
                  new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 =
                  new System.Text.RegularExpressions.Regex(@"<img[^>]+>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 =
                  new System.Text.RegularExpressions.Regex(@"</p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 =
                  new System.Text.RegularExpressions.Regex(@"<p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 =
                  new System.Text.RegularExpressions.Regex(@"<[^>]*>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记    
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性    
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件    
            html = regex4.Replace(html, ""); //过滤iframe    
            html = regex5.Replace(html, ""); //过滤frameset    
            html = regex6.Replace(html, ""); //过滤frameset    
            html = regex7.Replace(html, ""); //过滤frameset    
            html = regex8.Replace(html, ""); //过滤frameset    
            html = regex9.Replace(html, "");
            //html = html.Replace(" ", "");   
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = Regex.Replace(html, "[\f\n\r\t\v]", "");  //过滤回车换行制表符   
            return new MvcHtmlString(html);

        }

        public static HtmlString GenerateLeftMenu(List<VRolePurview> roles, List<VRolePurview> sourceRoles, int deep)
        {
            string header = "<ul {1}>{0}</ul>";
            string itemstr = "<li><a class='{2}' href='{0}?OpID={7}' target='{1}' rel='{2}' fresh='{3}' External='{4}'>{5}</a>{6}</li>";
            string headerclass = string.Empty;
            headerclass = deep > 1 ? string.Empty : "class=\"tree treeFolder\"";
            string result = string.Empty;
            foreach (var m in roles.OrderBy(c=>c.Order))
            {
                string rel = m.ID;
                //if (!string.IsNullOrEmpty(m.Url))
                //{
                //    var navTabid = m.Url.Replace('/', ' ').Trim();
                //    var rels = navTabid.Split(' ').ToList();
                //    int index = rels.Count >= 4 ? 3 : 2;
                //    for (int i = 0; i < index; i++)
                //    { rel += rels[i]; }
                //}
                rel = rel ?? "Default";
                result += string.Format(itemstr,
                    m.Url,
                    m.Target,
                    rel,
                    !string.IsNullOrEmpty(m.Fresh) ? m.Fresh.ToLower() : "",
                    !string.IsNullOrEmpty(m.External) ? m.External.ToLower() : "",
                    m.ModuleName,
                    GenerateLeftMenu(sourceRoles.Where(c => c.Parent == m.ID && c.Types == 0).ToList(), sourceRoles, deep + 1),
                    m.ID
                    );
            }
            if (!string.IsNullOrEmpty(result))
            {
                result = string.Format(header, result, headerclass);
            }
            return new HtmlString(result);

        }
    }
}